package com.zy;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Random;

public class WatermarkExample {
    public static void main(String[] args) throws IOException {
        File file = new File("/Users/xxyy/picture/美女+车/5.jpg");
        BufferedImage image = ImageIO.read(file);
        Graphics2D g2d = image.createGraphics();

        Font font = new Font("Arial", Font.BOLD, 36);
        Color color = new Color(255, 255, 255, 128);
        g2d.setFont(font);
        g2d.setColor(color);

        String myIp = qqq.getLocalIP();
        FontMetrics fm = g2d.getFontMetrics();
        int x = image.getWidth() - fm.stringWidth(myIp) - 10;
        int y = image.getHeight() - fm.getHeight() - 10;
        g2d.drawString(myIp, x, y);
// 生成 Random 对象
        Random random = new Random();
        int number=0;
        for (int i = 0; i < 10; i++) {
            // 生成 0-9 随机整数
             number = random.nextInt(10);
           // System.out.println("生成随机数：" + number);
        }

        File output = new File("/Users/xxyy/picture/22/"+number+".jpg");
        ImageIO.write(image, "jpg", output);



    }

    public static String getClientIp(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
}